home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0039_Pythagorean triples.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  5.2 KB  |  116 lines

  1. {
  2. DP> Howdy everyone, my computer science class at my high school is trying
  3. DP> to work on the most efficient way to find all the pythagorean triples
  4. DP> from 1 to a certain number, then dump them into a text file.  Currently,
  5. DP> the fastest anyone has managed to get is 57 seconds for 1 to 1000.  The
  6. DP> program would be considerably faster, but we are removing all duplicates
  7. DP> and dilations.  In case you're wondering, a dilation would be: 6 8 10,
  8. DP> which is just a dilation of 3 4 5.  IF anyone has any ideas (actual code
  9. DP> would be nice) to help improve the speed of our programs, please drop me
  10. DP> a message!!!  Thanks a lot.
  11.  
  12.   This kinda piqued my curiosity so I tried just what you were describing.
  13.   I'm not much of a math wiz, but I came up with the following... it
  14.   doesn't check for dilations... too lazy for that now but I looked
  15.   at the pattern of dilations and there is a pretty definite pattern, so
  16.   it shouldn't be too hard to put in.
  17.  
  18.   The output is not neat or anything, and it uses DOS redirection to
  19.   output the numbers but I clocked it at 4 seconds on a DX2/66 in a full
  20.   screen DOS session under windows 3.1 so it's pretty fast even for not
  21.   checking dilations...
  22.  
  23.   The next message contains the rest of the code...
  24.  
  25.  
  26. { DEFINE DEBUG}                     { turn this on for debugging work   }
  27. {$A-}                               { turn off alignment                }
  28. {$B+}                               { complete boolean evaluations      }
  29.  
  30. {$IFNDEF DEBUG}                     { if not debugging program turn off }
  31. {$D-}                               { no debug info                     }
  32. {$R-}                               { turn off range checking           }
  33. {$ELSE}                             { else                              }
  34. {$D+}                               { turn on debug info                }
  35. {$R+}                               { turn on range checking            }
  36. {$ENDIF}                            { end conditional if                }
  37.  
  38. {$F+}                               { force far calls                   }
  39. {$G+}                               { enable 286 instructions           }
  40. {$N+}                               { enable coprocessor                }
  41. {$P-}                               { no open string                    }
  42. {$V+}                               { strict string checking            }
  43.  
  44. {-----------------------------------------------------------------------}
  45. { Program      : Triples                                                }
  46. { Last Modified: 03-23-96                                               }
  47. { Purpose      : To find all the pythagorean triples from 1 to 1000     }
  48. {-----------------------------------------------------------------------}
  49. Program PythagoreanTriples;
  50. Uses Crt,Timer;  { timer code at the END of this program !! }
  51.  
  52.  
  53. {-----------------------------------------------------------------------}
  54. { global constants                                                      }
  55. {-----------------------------------------------------------------------}
  56. Const
  57.       MaxNum = 1000;                { maximum number to find triple for }
  58.  
  59. {-----------------------------------------------------------------------}
  60. { global variables                                                      }
  61. {-----------------------------------------------------------------------}
  62. Var
  63.     ICtr, ICtr2: Word;              { iteration counters                }
  64.     Result: extended;
  65. {-----------------------------------------------------------------------}
  66. { main code here                                                        }
  67. {-----------------------------------------------------------------------}
  68. Begin                               { begin main block                  }
  69.   assign(output, '');
  70.   rewrite(output);
  71.   ClrScr;                           { clear the screen                  }
  72.   Clockon;
  73.   For ICtr := 1 to MaxNum Do        { go thorugh numbers                }
  74.     For ICtr2 := ICtr to MaxNum Do  { go through numbers                }
  75.       Begin                         { begin ICtr2 for loop              }
  76.         Result := Sqrt(ICtr*ICtr + ICtr2 * ICtr2);
  77.         if (Result - INT(Result) = 0)then
  78.           Writeln(output,ICtr,'   ', ICtr2:10,'   ', Result);
  79.       End;                          { end ICtr2 for loop                }
  80.   clockoff;
  81. End.                                { end main block                    }
  82. -------------------------------------------------------------------------
  83. here is the timer unit you need.
  84.  
  85. { Timing unit for optomizing code }
  86. unit TIMER;
  87.  
  88. interface
  89.  
  90.          procedure clockon;
  91.          procedure clockoff;
  92.  
  93. implementation
  94. uses dos;
  95.  
  96. var H,M,S,s100:word;
  97.     startclock,stopclock:real;
  98.  
  99.     procedure clockon;
  100.     begin
  101.          gettime(h,m,s,s100);
  102.          startclock := (H*3600)+(M*60)+S+(S100 / 100);
  103.          Writeln('Start time = ',Startclock:0:2);
  104.     end;
  105.     procedure clockoff;
  106.     begin
  107.          gettime(h,m,s,s100);
  108.          stopclock := (H*3600)+(M*60)+S+(S100 / 100);
  109.          writeln;
  110.          writeln('Stop time = ',stopclock:0:2);
  111.          writeln('Elapsed time = ',(stopclock-startclock):0:2);
  112.     end;
  113.  
  114. begin
  115. end.
  116.